package com.jiawa.wiki.controller;

import com.jiawa.wiki.response.CommonResp;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 富文本图片上传
 */
@RestController
public class FileUploadController {

    @Value("${filepath}")
    private String filePath;

    @PostMapping("/file-upload")
    public CommonResp<String> fileUpload(@RequestParam("file") MultipartFile file) throws IOException {
        CommonResp<String> commonResp = new CommonResp();
        String filename = file.getOriginalFilename();
        System.out.println("文件名: " + filename);
        // 上传图片到 web 目录下
        file.transferTo(new File(filePath + filename));
        String fileUrl = "http://localhost:8081/upload/" + filename;
        commonResp.setContent(fileUrl);
        return commonResp;
    }
}
